//
//  KLRechargepayPwdViewController.m
//  KISSLOCK
//
//  Created by 陈宾鹏 on 16/8/14.
//  Copyright © 2016年 陈宾鹏. All rights reserved.
//

#import "KLRechargepayPwdViewController.h"
#import "KLHelp.h"
#import "KLHttpAPI.h"
#import "FMDBTool.h"
#import "Masonry.h"
#import "HemaConst.h"
#import "JRToast.h"
#import "KLSetPayPasswordViewController.h"

@interface KLRechargepayPwdViewController ()
{
    int seconds;
}
@property(nonatomic,strong) UIView *superView;
@property(nonatomic,strong) UILabel *phoneLabel;
@property(nonatomic,strong) UILabel *sendCodelabel;
@property(nonatomic,strong) UITextField *codeTF;
@property(nonatomic,strong) UIButton *confirmBtn;

@property(nonatomic,strong) NSTimer *timer;
@end

@implementation KLRechargepayPwdViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    seconds = 60;
    [self naveSet];
    self.superView = [[UIView alloc]init];
    self.superView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.superView];
    [self.superView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(UI_View_Width);
        make.height.mas_equalTo(96);
        make.top.mas_equalTo(20);
        make.centerX.equalTo(self.view);
    }];
    
    NSString *phoneText = [[NSUserDefaults standardUserDefaults] objectForKey:@"phoneNumber"];
    self.phoneLabel = [[UILabel alloc]init];
    self.phoneLabel.text = [NSString stringWithFormat:@"%@*****%@",[phoneText substringToIndex:3],[phoneText substringFromIndex:8]];
    self.phoneLabel.textColor = RGB_UI_COLOR(200, 200, 200);
    self.phoneLabel.textAlignment = NSTextAlignmentLeft;
    self.phoneLabel.font = [UIFont systemFontOfSize:16];
    [self.superView addSubview:self.phoneLabel];
    [self.phoneLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(30);
        make.left.equalTo(self.superView).with.offset(15);
        make.top.equalTo(self.superView).with.offset(9);
    }];
    
    self.sendCodelabel = [[UILabel alloc]init];
    self.sendCodelabel.layer.masksToBounds = YES;
    self.sendCodelabel.layer.cornerRadius = 4;
    self.sendCodelabel.layer.borderWidth = 1.0f;
    self.sendCodelabel.layer.borderColor = RGB_UI_COLOR(51, 153, 255).CGColor;
    self.sendCodelabel.userInteractionEnabled = YES;
    self.sendCodelabel.text = @"发送验证码";
    self.sendCodelabel.textColor = RGB_UI_COLOR(51, 153, 255);
    self.sendCodelabel.font = [UIFont systemFontOfSize:16];
    self.sendCodelabel.textAlignment = NSTextAlignmentCenter;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap:)];
    [self.sendCodelabel addGestureRecognizer:tap];
    [self.superView addSubview:self.sendCodelabel];
    [self.sendCodelabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.superView).with.offset(9);
        make.height.mas_equalTo(30);
        make.right.equalTo(self.superView).with.offset(-10);
        make.width.mas_equalTo(85);
    }];
    
    UIView *lineView = [[UIView alloc]init];
    lineView.backgroundColor = RGB_UI_COLOR(220, 220, 220);
    [self.superView addSubview:lineView];
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(0.8);
        make.top.equalTo(self.superView).with.offset(48);
        make.left.equalTo(self.superView).with.offset(10);
        make.width.mas_equalTo(UI_View_Width-10);
    }];
    
    self.codeTF = [[UITextField alloc]init];
    self.codeTF.placeholder = @"请输入手机短信中的验证码";
    self.codeTF.borderStyle = UITextBorderStyleNone;
    self.codeTF.keyboardType = UIKeyboardTypeNumberPad;
    [self.superView addSubview:self.codeTF];
    [self.codeTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.superView).with.offset(15);
        make.right.equalTo(self.superView).with.offset(-15);
        make.height.mas_equalTo(30);
        make.bottom.equalTo(self.superView).with.offset(-9);
    }];
    
    self.confirmBtn = [[UIButton alloc]init];
    [self.confirmBtn setTitle:@"验证" forState:UIControlStateNormal];
    self.confirmBtn.backgroundColor = [KLHelp mainBlueColor];
    self.confirmBtn.layer.masksToBounds = YES;
    self.confirmBtn.layer.cornerRadius = 5.0f;
    self.confirmBtn.titleLabel.text = @"验证";
    self.confirmBtn.titleLabel.font = [UIFont systemFontOfSize:23];
    [self.confirmBtn addTarget:self action:@selector(confirmCode:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.confirmBtn];
    [self.confirmBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(UI_View_Width - 30);
        make.left.equalTo(self.view).with.offset(15);
        make.height.mas_equalTo(50);
        make.top.equalTo(self.superView).with.offset(115);
    }];
}

//发送验证码
-(void)tap:(UITapGestureRecognizer *)sender{
    //请求验证码
    NSString *username = [[NSUserDefaults standardUserDefaults] objectForKey:@"phoneNumber"];
    [KLHelp showHUDAddedTo:self.view title:@"请稍候..." details:nil animated:YES];
    [KLHttpAPI requestSecurityCodeWithUserName:username type:@"0" completionHandler:^(NSString *responseString) {
        NSLog(@"请求正常返回:%@",responseString);
        [KLHelp hideAllHUDsForView:self.view animated:YES];
        NSDictionary *dic = [KLHelp dictionaryWithJsonString:responseString];
        NSString *successStr = [dic objectForKey:@"success"];
        if(successStr.integerValue == 1){
            //启动定时器
            self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(updateLabel) userInfo:nil repeats:YES];
            [JRToast showWithText:@"验证码已发送" bottomOffset:50 duration:1.5];
            self.sendCodelabel.backgroundColor =  [UIColor lightGrayColor];
            self.sendCodelabel.userInteractionEnabled = NO;
        }else{
            [JRToast showWithText:[dic objectForKey:@"msg"]];
        }
    } errorHandler:^(NSString *error) {
        NSLog(@"请求错误;%@",error);
        [KLHelp hideAllHUDsForView:self.view animated:YES];
    }];
}-(void)updateLabel{
    seconds--;
    if(seconds >= 0){
        NSMutableAttributedString *secondsStr = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"%d秒重发",seconds]];
        
        [secondsStr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, 2)];
        [secondsStr addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"American Typewriter" size:17] range:NSMakeRange(0, 2)];
        
        self.sendCodelabel.attributedText = secondsStr;
    }else{
        self.sendCodelabel.userInteractionEnabled = YES;
        self.sendCodelabel.text = @"发送验证码";
        self.sendCodelabel.backgroundColor = [UIColor clearColor];
        seconds = 60;
        [self.timer invalidate];
    }
}

//验证按钮
-(void)confirmCode:(UIButton *)sender{
    if(self.codeTF.text.length == 0){
        [JRToast showWithText:@"验证码不能为空"];
        return ;
    }
    [KLHttpAPI requestCheckSecurityCodeWithUserName:[[NSUserDefaults standardUserDefaults] objectForKey:@"phoneNumber"] securityCode:self.codeTF.text completionHandler:^(NSString *responseString) {
        NSDictionary *dic = [KLHelp dictionaryWithJsonString:responseString];
        NSString *isSuccess = [dic objectForKey:@"success"];
        if(isSuccess.integerValue == 1){
            KLSetPayPasswordViewController *setPayPwdVC = [[KLSetPayPasswordViewController alloc]init];
            [self.navigationController presentViewController:setPayPwdVC animated:YES completion:nil];
        }else{
            [JRToast showWithText:[dic objectForKey:@"msg"]];
        }
    } errorHandler:^(NSString *error) {
    }];
}

#pragma mark - 界面设置
//初始化导航栏
-(void)naveSet{
    self.view.backgroundColor = RGB_UI_COLOR(242, 242, 242);
    self.navigationController.navigationBarHidden = NO;
    self.title = @"找回支付密码";
    
    [KLHelp navSet:self doBackHandeler:^(UIButton *backButton) {
        [backButton addTarget:self action:@selector(doBack:) forControlEvents:UIControlEventTouchUpInside];
    }];
}-(void)doBack:(id)sender{
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)dealloc{
    if(self.timer != nil){
        [self.timer invalidate];
        self.timer = nil;
    }
    
}
@end
